package com.graduation.roombooking.controller.web;

import com.graduation.roombooking.service.IRoomService;
import com.graduation.roombooking.service.UserService;
import com.graduation.roombooking.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/5/4 14:51
 * @Version:
 * @Description:TODO
 */
@RestController
@RequestMapping("/web/room")
@Api(tags = "web——会议室模块")
public class WebRoomController {
    @Autowired
    private UserService userService;
    @Autowired
    private IRoomService roomService;

    @ApiOperation("获取会议室列表，本部门只能展示本部门的会议室")
    @ResponseBody
    @PostMapping("/list")
    public RespBean getList(@RequestBody String ticket, HttpServletRequest request, HttpServletResponse response){
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                if(userVo.getAction().equals("system_administrator")) {
                    return roomService.getList(null);
                }else if(userVo.getAction().equals("organization_administrator")||userVo.getAction().equals("organization_member")){
                    return roomService.getList(userVo.getCompanyName());
                }
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法查看");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("添加会议室，只能部门管理员操作")
    @ResponseBody
    @PostMapping("/add")
    public RespBean addOne(@RequestBody AddRoomVo addRoomVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=addRoomVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()) {
            //根据cookie获取用户信息
            UserVo userVo = userService.getUserByCookie(ticket, request, response);
            if (userVo != null) {
                if(userVo.getAction().equals("organization_administrator")){
                    return roomService.addOne(addRoomVo,userVo.getCompanyName());
                }
                return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非组织管理员，无法添加");
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("修改会议室基本信息")
    @ResponseBody
    @PostMapping("/update/baseInfo")
    public RespBean updateBaseInfo(@RequestBody UpdateRoomBaseInfoVo updateRoomBaseInfoVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=updateRoomBaseInfoVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()) {
            //根据cookie获取用户信息
            UserVo userVo = userService.getUserByCookie(ticket, request, response);
            if (userVo != null) {
                if(userVo.getAction().equals("organization_administrator")){
                    return roomService.updateBaseInfo(updateRoomBaseInfoVo,userVo.getCompanyName());
                }
                return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非组织管理员，无法修改");
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("删除会议室（需要谨慎操作，同时会删除该会议室下的设备）")
    @ResponseBody
    @PostMapping("/delete")
    public RespBean deleteOne(@RequestBody DeleteVo deleteVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=deleteVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()) {
            //根据cookie获取用户信息
            UserVo userVo = userService.getUserByCookie(ticket, request, response);
            if (userVo != null) {
                if(userVo.getAction().equals("organization_administrator")){
                    return roomService.deleteOne(deleteVo.getId(),userVo.getCompanyName());
                }
                return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非组织管理员，无法修改");
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("联合查询(本部门不需要传部门名称)")
    @ResponseBody
    @PostMapping("/union/query")
    public RespBean unionQuery(@RequestBody RoomUnionQuery roomUnionQuery,HttpServletRequest request, HttpServletResponse response){
        String ticket=roomUnionQuery.getTicket();
        if(ticket!=null && !ticket.isEmpty()) {
            //根据cookie获取用户信息
            UserVo userVo = userService.getUserByCookie(ticket, request, response);
            if(userVo!=null ){
                if(userVo.getAction().equals("system_administrator")) {
                    return roomService.unionQuery(roomUnionQuery,null);
                }else if(userVo.getAction().equals("organization_administrator")||userVo.getAction().equals("organization_member")){
                    return roomService.unionQuery(roomUnionQuery,userVo.getCompanyName());
                }
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("系统管理员修改会议室状态")
    @ResponseBody
    @PostMapping("/sys/update")
    public RespBean sysUpdateStatus(@RequestBody SysUpdateStatusVo sysUpdateStatusVo, HttpServletRequest request, HttpServletResponse response){
        String ticket=sysUpdateStatusVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()) {
            //根据cookie获取用户信息
            UserVo userVo = userService.getUserByCookie(ticket, request, response);
            if (userVo != null) {
                if(userVo.getAction().equals("system_administrator")){
                    return roomService.sysUpdateStatus(sysUpdateStatusVo.getId());
                }
                return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法修改");
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

}
